package popcorn.ticket.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import popcorn.ticket.entity.Logs;
import popcorn.ticket.entity.MovieReview;
import popcorn.ticket.entity.Users;
import popcorn.ticket.service.MovieReviewService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

/**
 * 追评表控制器
 * 
 * @author 陈雄伟
 *
 */
@Controller
public class MovieReviewController {
	@Autowired
	private MovieReviewService mrs;

	/**
	 * 模糊查询追评表的信息+分页
	 * 
	 * @param keywords 关键字
	 * @param page     页码
	 * @param limit    每页的数量
	 * @return pagedate
	 */
	@RequestMapping(value = "gmrbl", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<MovieReview> getMovieReviewByLike(
			@RequestParam(value = "keywords", defaultValue = "") String keywords,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "select", defaultValue = "") String select,
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		PageData<MovieReview> pd = null;
		if (select.equals("username")) {
			// 随便赋个值让他不为空即可
			String username = "1";
			pd = mrs.getMovieReviewByLike(keywords, page, limit, username, "", startTime, endTime, 0);
		} else if ("moviename".equals(select)) {
			String moviename = "1";
			pd = mrs.getMovieReviewByLike(keywords, page, limit, "", moviename, startTime, endTime, 0);
		} else if ("".equals(select)) {
			pd = mrs.getMovieReviewByLike(keywords, page, limit, "", "1", startTime, endTime, 0);
		} else if ("userid".equals(select)) {
			int userid = 1;
			pd = mrs.getMovieReviewByLike(keywords, page, limit, "", "", startTime, endTime, userid);
		}

		return pd;
	}


	/**
	 * 批量修改显示状态
	 * 
	 * @param idlist
	 * @return AjaxResponse对象
	 * @throws UnknownHostException
	 */
	@RequestMapping(value = "umrs", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public AjaxResponse updateMRStatu(@RequestBody String idlist) {

		// 定义一个ArrayList集合来存放要批量禁用的id集合
		List list = new ArrayList<>();
		//正则表达式判断是否为数字，为数字则说明是单个删除，不是的话就是批量删除
		if (idlist.matches("[0-9]+")) {
			list.add(idlist);
		} else {
			// 将前台的数据进行剪切，在存入list集合中
			idlist = idlist.substring(0, idlist.length() - 2);
			idlist = idlist.substring(1, idlist.length());
			String[] arr = idlist.split(",");
			for (int i = 0; i < arr.length; i++) {

				list.add(arr[i]);

			}
		}
		// 书写日志内容，将日志的对象拿给serviceimpl使用
		Logs log = new Logs();
		try {
			log.setLogip(InetAddress.getLocalHost() + "");
			log.setLogobject("MovieReview");
			log.setLogop("禁用评论");
			log.setRemark("用户删追评");
			log.setLogtime(new Date());
			Users users = new Users();
			users.setUserid(1);
			log.setUsers(users);
			// 执行方法返回数据
			AjaxResponse aar = mrs.updateMRSta(list, log);
			return aar;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			AjaxResponse ar = new AjaxResponse();
			ar.setMsg("禁用失败");
			ar.setCode(0);
			return ar;
		}
	}
	
	@RequestMapping(value = "delReviewByUid", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse delReviewByUid(int reviewid,int userid) {
		Logs log = new Logs();
		try {
			log.setLogip(InetAddress.getLocalHost() + "");
			log.setLogobject("MovieReview");
			log.setLogop("禁用追评");
			log.setRemark("用户删追评");
			log.setLogtime(new Date());
			Users users = new Users();
			users.setUserid(userid);
			log.setUsers(users);
			// 执行方法返回数据
			AjaxResponse ar = mrs.updateMRStaByUser(reviewid, log);
			return ar;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			AjaxResponse ar = new AjaxResponse();
			ar.setMsg("删除失败");
			ar.setCode(0);
			return ar;
		}	
	}
	
	@RequestMapping(value = "gmvbci/{commentid}", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<MovieReview> getMVByCommentid(@PathVariable(value = "commentid") int commentid,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "6") int limit) {
		PageData<MovieReview> pd = mrs.getMVByCommentId(commentid, page, limit);
		return pd;
	}
	
	@RequestMapping(value = "addreview",produces = "application/json;charset=utf-8",consumes = "application/json")
	@ResponseBody
	public AjaxResponse addReview(@RequestBody MovieReview mr) {
		mr.setReviewtime(new Date());
		mr.setReviewstatus(1);
		return mrs.addReviewByUser(mr);
	}
}
